package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.base.BaseWebActivity.LoadType;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.utils.HSJSONTools;
import com.hundsun.yr.universal.library.view.itemview.SettingView;
import com.hundsun.yr.universal.library.view.itemview.SettingView.onSettingViewItemClickListener;
import com.hundsun.yr.universal.library.view.itemview.SettingViewItemData;
import com.hundsun.yr.universal.p2p.bean.enums.EnumProtocol;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.interactor.impl.MoreInteractorImpl;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.ItemViewPresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.view.base.BaseItemView;

import java.util.List;


/**
 * @version V1.0
 * @Title: MineMoreActivity.java
 * @Description: 更多
 * @date 2015-12-4 下午2:17:04
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class MineMoreActivity extends BaseActivity implements BaseItemView, onSettingViewItemClickListener {

    private RequestPresenter mPresenter;
    private SettingView setting_view;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_more;
    }

    @Override
    protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        getSupportActionBar().setTitle("");
        setToolbarTitle(R.string.more);
        setting_view = findView(R.id.setting_view);
        mPresenter = new ItemViewPresenterImpl(this, this, new MoreInteractorImpl());
        mPresenter.initialized();
    }

    @Override
    protected void onNetworkConnected(NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @Override
    public void onItemClick(int index) {
        if (isFastDoubleClick()){
            return;
        }

        if (!NetUtils.isNetworkConnected(mContext)){
            showProgressToDismiss();
            return;
        }

        if (index == 4) {
            Bundle bundle = new Bundle();
            String[] urlArr = getResources().getStringArray(R.array.more_skip_urls);
            String[] titles = getResources().getStringArray(R.array.more_titles);
            bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, titles[index]);
            bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, getString(R.string.root_h5_url_test) + urlArr[index]);
            bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, LoadType.HTTP.getIntValue());
            bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
            readyGo(WebLoanActivity.class, bundle);
        } else {
            Bundle bundle = new Bundle();
            String[] urlArr = getResources().getStringArray(R.array.more_skip_urls);
            String[] titles = getResources().getStringArray(R.array.more_titles);
            bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, titles[index]);
            bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, getString(R.string.root_h5_url_test) + urlArr[index]);
            bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, LoadType.HTTP.getIntValue());

            bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
            readyGo(WebActivity.class, bundle);
        }


    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        // TODO Auto-generated method stub
        if (TextUtils.equals(TaskConstants.TASK_QUERYOSSWORD, task_name)) {
            String mWebUrl = HSJSONTools.getString((String) data, "url");

            Bundle bundle = new Bundle();
            bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, getString(R.string.more_help));
            bundle.putString(WebProtocolActivity.PROTOCOL_CODE, EnumProtocol.findCode(EnumProtocol.HELP));
            bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, mWebUrl);
            bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, LoadType.HTTP.getIntValue());
            bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
            readyGo(WebActivity.class, bundle);
        }
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> void setAdapter(List<T> data) {
        // TODO Auto-generated method stub
        setting_view.setAdapter((List<SettingViewItemData>) data);
        setting_view.setOnSettingViewItemClickListener(this);
    }

    @Override
    public <T> void initializeError(String task_name, T data) {
        // TODO Auto-generated method stub

    }

    private void showProgressToDismiss() {
        showProgress("");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                closeProgress();
            }
        }, 2000);
    }


    /**
     * 防止按钮连续点击
     */
    private static long lastClickTime;
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
}
